package com.dianbo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.ConnectDB;
import util.TotalClass;
import util.UtilDAO;

import com.BlackPhoneModule.bean.BlackPhoneBean;
import com.dianbo.bean.Dianbo;

public class DianboDao {

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-4
	 *下午02:42:34
	 *zzClub
	 *com.dianbo.dao
	 */
	private ResultSet rs =null;
	private String sql = "";
	private List<Dianbo> dianboList = null;
	// 数据库连接
	private Connection conn = null;
	// st对象
	private PreparedStatement pt = null;

	private Statement st = null;
	/**
	 * 
	 *@author qingyu zhang
	 *@function:
	 * @param table
	 * @param order
	 * @param condition
	 * @return
	 *2011-4-12
	 *下午09:42:53
	 *zzClub
	 *com.dianbo.dao
	 *List<Dianbo>
	 */
	public List<Dianbo> getDianboList(String table,String order,String condition){
		sql = UtilDAO.getList(table, order, condition);
		rs = new TotalClass().getResult(sql);
		System.out.println("sql||"+sql);
		try {
			while(rs.next()){
				if(dianboList==null){
					dianboList = new ArrayList<Dianbo>();
				}
				dianboList.add(this.getDianbo(rs));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dianboList;
	}
	public Dianbo getDianbo(ResultSet rs){
		Dianbo dianbo = new Dianbo();
		try {
			dianbo.setQsContent(rs.getString("qsContent"));
			dianbo.setQsInfoId(rs.getInt("qsInfoId"));
			dianbo.setQsIntegral(rs.getString("qsIntegral"));
			dianbo.setQsUpPhone(rs.getString("qsUpPhone"));
			dianbo.setQsUpTime(rs.getString("qsUpTime"));
			dianbo.setReceiveId(rs.getInt("receiveId"));
			dianbo.setTypeId(rs.getString("typeId"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dianbo;
	}
	public boolean modifyState(String dianboId){
		boolean bo = false;
		String hql = "update tb_send_dianbo set state=1 where dianboId in("+dianboId+")";
		bo = new TotalClass().operatorObject(hql);
		return bo;
	}
	/**
	 * 
	 *@author qingyu zhang
	 *@function:分拣时存放点播
	 * @param dianbo
	 * @return
	 *2011-4-13
	 *上午08:40:05
	 *zzClub
	 *com.dianbo.dao
	 *boolean
	 */
	public boolean addDianbo(Dianbo dianbo){
		boolean bo = false;
		sql="insert into tb_qs_info(receiveId,typeId,qsContent,qsUpPhone,qsUpTime) " +
				"values("+dianbo.getReceiveId()+",'"+dianbo.getTypeId()+"','"+dianbo.getQsContent()+"','"+dianbo.getQsUpPhone()+"','"+dianbo.getQsUpTime()+"')";
		System.out.println("sql:||insert|||"+sql);
		bo = new TotalClass().operatorObject(sql);
		return bo;
	}
	public boolean saveDianboList(List<Dianbo> dianboList){
		boolean bo = false;
		try
		{
			conn = ConnectDB.getSqlServerConnection();
			st = conn.createStatement();
			if(dianboList!=null&&dianboList.size()>0){
				for(Dianbo dianbo:dianboList){
					String hql ="insert into tb_qs_info(receiveId,typeId,qsContent,qsUpPhone,qsUpTime) " +
					"values("+dianbo.getReceiveId()+",'"+dianbo.getTypeId()+"','"+dianbo.getQsContent()+"','"+dianbo.getQsUpPhone()+"','"+dianbo.getQsUpTime()+"')";
					System.out.println("hql==="+hql);
					st.addBatch(hql);
				}
				st.executeBatch();
				bo = true;
			}
		}catch(Exception e){
			e.printStackTrace();
			bo = false;
		}finally{
			ConnectDB.closeSqlConnection();
			return bo ;
		}
	}
	/**
	 * 单条记录删除
	 * jhc
	 * 2011-1-27 下午02:30:38
	 */
	public boolean del(String tableName,String keyId,String idList){
		boolean bo = UtilDAO.del(tableName, keyId, idList);
		return bo ;
	}
	/**
	 * 多项删除
	 * jhc
	 * 2011-1-27 下午02:30:38
	 */
	public boolean delin(String tableName,String keyId,String idList){
		boolean bo = UtilDAO.delin(tableName, keyId, idList);
		return bo ;
	}
}
